package com.sky.web.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 菜品添加
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result add(@RequestBody DishDTO dishDTO) {
        dishService.saveWithFlavor(dishDTO);
        //清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        redisTemplate.delete(key);
        return Result.success();
    }

    /**
     * 分页查询
     * @param dishPageDTO
     * @return
     */
    @GetMapping("/page")
    public Result page(DishPageDTO dishPageDTO) {
        PageResult pageResult = dishService.page(dishPageDTO);
        return Result.success(pageResult);

    }

    /**
     * 菜品删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(Long[] ids) {
        List<Dish> list = dishService.listByIds(ids);
        dishService.deleteBatch(ids);
        for (Dish dish : list) {
            redisTemplate.delete("dish_"+dish.getCategoryId());
        }
        return Result.success();
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 修改
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {
        //查询数据库中关联的分类id
        Dish dish = dishService.getById(dishDTO.getId());
        dishService.updateWithFlavor(dishDTO);
        //清理缓存
        redisTemplate.delete("dish_"+dish.getCategoryId());
        if (dish.getCategoryId()!=dishDTO.getCategoryId()){
            //说明修改了分类，新的分类也要清理
            redisTemplate.delete("dish"+dishDTO.getCategoryId());
        }
        return Result.success();
    }

    /**
     * 起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result enableOrDisable(
            @PathVariable Integer status,
            @RequestParam Long id
    ) {
        dishService.enableOrDisable(status,id);
        //清理缓存
        Dish dish=dishService.getById(id);
        redisTemplate.delete("dish_"+dish.getCategoryId());
        return Result.success();
    }
    //根据分类ID查询启售菜品列表
    @GetMapping("/list")
    public Result listById(@RequestBody Long categoryId) {
        DishDTO dishDTO = DishDTO.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return Result.success(dishService.listById(dishDTO));
    }
}
